'''
VISSS FRONT END:
    It has super cow power :)
    It uses the *settings.start_urls* to call
    the method *start_urls* of manager
'''
import stackless
import argparse,textwrap
import settings

import sys
import visss
from visss.objects import *

import manager

try :
    if sys.argv[1]=='moo' :
        print textwrap.dedent('''\
                  ______________ 
                 < THIS IS VISSS >
                  -------------- 
                  \   ^__^
                  \  (oo)\_______
                     (__)\       )\/\\
                         ||----w |
                         ||     ||

                  Okey, this is not apt-get.
                  But have you mooed today?
                   _____________ 
                   < use python! >
                    ------------- 
                    \   ^__^
                     \  (oo)\_______
                        (__)\       )\/\\
                            ||----w |
                            ||     ||
                   and be pythonic :) 
        ''')

        sys.exit()
except :
    pass

parser = argparse.ArgumentParser(
        description='VISSS stands for Viss Is a Simple Stackless Scraping-framework',
        epilog = textwrap.dedent('''\
                commands:
                    start : start the scraping

                VISSS has super cow power. Wish your have fun.
        '''),
        formatter_class=argparse.RawDescriptionHelpFormatter,
        prog='VISSS'
        )

parser.add_argument('command',
        choices=('start',),
        help='see Below commands for details'
    )

parser.add_argument('-e','--enable',dest='enabled_list',nargs='*',type=str,default=[],
    help='only enable the controller in the enable_list')

parser.add_argument('-d','--disable',dest='disabled_list',nargs='*',type=str,default=[],
        help='only disable the controller in the disable_list')

args=parser.parse_args()

enabled_list=args.enabled_list
disabled_list=args.disabled_list

if enabled_list :
    method = 'enable'

elif disabled_list :
    method = 'disable'

else :
    method=None

if args.command == 'start':
        
    manager_=manager.Manager(method,enabled_list,disabled_list)

    manager_.start_urls(settings.start_urls)

    result_channel=manager_.result_channel
    channel=manager_.channel

    try :
        x=result_channel.receive()
        print x
    except KeyboardInterrupt:
        channel.send(Event('Shutdown'))
        sys.exit()
else :
    raise NotImplementedError
